<?php

namespace App\Http\Admin\Services;

use App\Exceptions\InvalidRequestException;

use App\Http\Admin\Models\Accounts;
use Illuminate\Support\Facades\DB;
use App\Lib\PHPExcel\ExcelHandle;

class AccountsService extends BaseService
{

    public function list($conditions = [])
    {
        $raw = DB::raw("
            a.app_id,a.platform,a.account_name,a.account_login,
            a.account_password,a.cookie,a.phone,a.email,a.remark,a.is_special,
            d.collect_date,d.fans_count,d.fans_increase,d.score,d.publish_count,
            d.read_count,d.play_count,d.month_profit,d.day_profit,d.article_origin,
            d.video_origin,d.vflag,d.can_mount_shop,d.org_name,d.bj_fbsl_ytj,
            d.bj_fbsl_yc,d.bj_nrzl,d.bj_yhxa,d.bj_app_level_desc,d.bj_pubish_limit,
            d.tbdr_rz,d.tbdr_ly,d.tbdr_ssjg,d.tbdr_llcs,d.tbdr_ydcs,d.created_at,d.updated_at
        ");
        $query = Accounts::query();
        $query->from('admin_accounts as a')
            ->select($raw)
            ->leftJoin('admin_account_detail as d', 'a.app_id', '=', 'd.app_id');
        if (!empty($conditions['platform'])) {
            $query->where('a.platform', $conditions['platform']);
        }
        if (!empty($conditions['is_special'])) {
            $query->where('a.is_special', $conditions['is_special']);
        }
        if(!empty($conditions['has_cookie'])){
            $query->where('a.cookie', '!=', '');
        }

        if(!empty($conditions['account_name'])){
            $query->where('a.account_name', 'like', "%".$conditions['account_name']."%");
        }

        if(!empty($conditions['key'])){
            $query->orderBy('d.'.$conditions['key'],$conditions['order']);
        }
        $items = $query->orderBy('remark','asc')->get();

        foreach ($items as $item) {
            if ($item['cookie']) {
                $item['cookie_arr'] = $item['cookie'];
                $cookies = json_decode($item['cookie'], true);
                $item['cookie'] = '';
                foreach ($cookies as $c) {
                    $item['cookie'] .= $c['name'] . '=' . $c['value'] . ';';
                }
            }
        }
        return $items;
    }

    public function accountsTask($platform)
    {

        $arr = ExcelHandle::read();
        if (count($arr) == 0 || empty($platform)) {
            throw new InvalidRequestException('数据不能为空');
        }


        //解析数组
        $tmp_arr = [];
        foreach ($arr as $k => $v) {
            if (!empty($v['A'])) {
                $tmp_arr[$k]['app_id'] = $v['A'];
                $tmp_arr[$k]['account_name'] = $v['B'];
                $tmp_arr[$k]['account_login'] = $v['C'];
                $tmp_arr[$k]['account_password'] = $v['D'];
                $tmp_arr[$k]['phone'] = $v['E'];
                $tmp_arr[$k]['email'] = $v['F'];
                $tmp_arr[$k]['remark'] = $v['G'];
                $tmp_arr[$k]['desc'] = $v['H'];
                $tmp_arr[$k]['platform'] = $platform;
            }

        }

        //入库
        if (!empty($tmp_arr)) {
            $ids = collect($tmp_arr)->pluck('app_id');
            $existsAccounts = Accounts::whereIn('app_id', $ids)->where('platform', $platform)->pluck('app_id')->toArray();

            if (count($existsAccounts) > 0) {
                //查询数据库中不存在项
                $result = collect($tmp_arr)->whereNotIn('app_id', $existsAccounts)->values()->all();

                Accounts::insert($result);

            } else {

                Accounts::insert($tmp_arr);

            }

        }

        return $tmp_arr;

    }


    public function store($attributes)
    {
        $result = true;
        $attributes = json_decode($attributes, true);
        if (empty($attributes['app_id'])) {
            $result = false;
            return $result;
        }
        Accounts::where('app_id', $attributes['app_id'])->update($attributes);
        return $result;

    }

    public function cookieSet($attributes = [])
    {
        if(empty($attributes['app_id'])){
            throw new InvalidRequestException('需要appid');
        }

        Accounts::where('app_id', $attributes['app_id'])->update([
            'cookie' => $attributes['cookie']
        ]);
        return true;
    }

}